var t = getApp(),
  a = t.requirejs("core");
Page({
  data: {
    icons: t.requirejs("icons"),
    status: "",
    list: [],
    mtype:1,
    page: 1,
    cancel: ["我不想买了", "信息填写错误，重新拍", "其他原因"], //订单取消理由
    cancelindex: 0,
    alert_color: t.config.alert_color,
    button_color: t.config.button_color
  },
  onLoad: function (e) {
    a.setting();
    "" == t.getCache("userinfo") && wx.redirectTo({
      url: "/pages/error/index"
    }),
      console.log(e)
    this.setData({
      options: e,
      status: e.status || "",
      mtype: e.mtype || 1,
      alert_color: getApp().config.alert_color,
      button_color: getApp().config.button_color
    }),
      this.get_list()

  },
  
  /**
   *获取订单列表
   * @param status uid page
   * @return array
   */
  get_list: function () {
    var t = this;
    t.setData({
      loading: true
    }),
      a.get("Miaosha/OrderList", {
        page: t.data.page,
        status: t.data.status,
      mtype:t.data.mtype,
        uid: getApp().getCache("userinfo").uid
      }, function (e) {
        0 == e.code ? (t.setData({
          loading: false,
          show: true,
          total: e.info.length,
          //empty: true
        }), 
       
        e.info.length > 0 && t.setData({
          page: t.data.page + 1,
          list: t.data.list.concat(e.info)
        }), 
        e.info.length < 10 && t.setData({
          loaded: true
        })) : a.alert(e.msg)
      }, this.data.show)
  },
  
  /**
   *不同订单状态之间切换
   */
  selected: function (t) {
    var e = a.data(t).type;
    this.setData({
      list: [],
      page: 1,
      status: e,
      //empty: false
    }),
      this.get_list()
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1
    }),
      this.get_list()
    wx.stopPullDownRefresh();
  },
  //上拉加载更多
  onReachBottom: function () {
    this.data.loaded || this.get_list()
  },
  
  /**
   *取消订单
   * @param order_id buyer_id cancel_text
   * @return string
   */
  cancel: function (t) {
    var that=this,
     cancel_text=this.data.cancel[t.detail.value],
     s = a.data(t).orderid;
    a.get("Miaosha/CancelOrder", {
      order_id: s,
      cancel_text: cancel_text,
      buyer_id: getApp().getCache("userinfo").uid
    }, function (e) {
      0 == e.code ? (
        that.setData({
          page: 1,
          list: []
        }), 
        that.get_list(),
        a.alert('取消订单成功！')
      ) : a.alert(e.msg)
      })
    
  },
  
  /**
   *删除订单
   * @param order_id buyer_id 
   * @return string
   */
  delete: function (t) {
    var that = this,
    i = a.data(t).orderid;
    a.get("Miaosha/DelOrder", {
      order_id: i,
      buyer_id: getApp().getCache("userinfo").uid
    }, function (e) {
      0 == e.code ? (
        that.setData({
          page: 1,
          list: []
        }), that.get_list(),
        a.alert('删除订单成功！')
        
      ) : a.alert(e.msg)
      }, )
    
  },
  /**
   *退款
   * @param order_id 
   * @return string
   */
  refund: function (i) {
    var that = this,
      order_id = a.data(i).orderid;
    a.confirm("申请退款后请联系客服", function () {
      a.get("Miaosha/RefundOrder", {
        order_id: order_id,
        buyer_id: getApp().getCache("userinfo").uid
      }, function (t) {
        0 == t.code ? (
          that.setData({
            page: 1,
            list: []
          }), that.get_list(),
          a.toast('申请成功')
        ) : (a.alert(t.msg))
      });
    })
  },
  to_home:function(){
    a.jump('/yb_shop/pages/index/index');
  },
  /**
   *确认收货
   * @param order_id buyer_id 
   * @return string
   */
  finish: function (t) {
    var that=this,
    s =  a.data(t).orderid;
    a.get("Miaosha/SignOrder", {
      order_id: s,
      buyer_id: getApp().getCache("userinfo").uid
    }, function (e) {
      0 == e.code ? (
        that.setData({
          page: 1,
          list: []
        }), that.get_list(),
        a.alert('使用成功！')
      ) : a.alert(e.msg)
      })
  }
})